import wx
import wx.media
from Constant import *
from PublicMethod import *
'''
Frame_Main : 
    The main selection of this game, the selection includes as follow :
    1. start a new game
    2. open frame of edit map
    3. open option to set some arguments of this game
    4. open help
    5. quit game
'''
class Frame_Main(wx.Panel):
    '''
    Constructor : Frame_Main
        parent : add panel to this
    '''
    def __init__(self, parent):
        parent.ACT002_setSize((800, 650))
        wx.Panel.__init__(self, parent, -1, size = (parent.GetSize()[0], parent.GetSize()[1] - 20))
        self.SetBackgroundColour("black")
        self._001_initialInstanceVariable(parent)
        self._002_bindAction()
        self._003_createBackground()
        self._006_createTitleImg()
        self._004_createSelection()
        self._005_createPeopleImg()
        self.timer_manAnimation.Start(1000)
        if self.main.sound != None and self.main.sound.GET003_curPlayMusicName() != str(MUSIC_TITLE) :
            self.main.ACT003_playBackgroundMusic(MUSIC_TITLE)
        parent.SetFocus()
    '''
    function _001 : initialize the variables used for this object
        main : the main frame
    '''
    def _001_initialInstanceVariable(self, main):
        #pointer of main frame
        self.main = main
        #current index of selection
        self.curChooseSel = 0
        #current image index of man image
        self.curManImgIndex = 0
        #image of background
        self.imgBackground = None
        #image of man
        self.imgMan = None
        #image of diamond
        self.imgDiamond = None
        #image of title
        self.imgTitle = None
        #image button list of selection
        self.list_btnSelection = []
        #the timer of changing image of man
        self.timer_manAnimation = wx.Timer()
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        self.main.Bind(wx.EVT_KEY_DOWN, self.SLOT001_OnKeyClick)
        self.timer_manAnimation.Bind(wx.EVT_TIMER, self.SLOT002_animationMan)
    '''
    function _003 : create background image of this frame
    '''
    def _003_createBackground(self):
        image = PublicMethod.ACT001_getBmpImage(FILE_NAME_BACKGROUND, self.GetSize()[0], self.GetSize()[1])
        self.imgBackground = wx.StaticBitmap(parent=self, bitmap=image)
        self.imgBackground.Show(False)
        self.imgBackground.Show(True)
    '''
    function _004 : create selection image
    '''
    def _004_createSelection(self):
        index = 0
        for img1, img2 in LIST_SELECTION_IMG :
            if index == self.curChooseSel :  #selected image
                fileName = DIR_IMG_MAIN_FRAME + "/" + img2
            else :  #unselected image
                fileName = DIR_IMG_MAIN_FRAME + "/" + img1
            image = PublicMethod.ACT001_getBmpImage(fileName)
            pos = ((self.GetSize()[0] - image.GetSize()[0]) / 2, 
                    self.GetSize()[1] - (10 + (LIST_SELECTION_IMG.__len__() - index) * (image.GetSize()[1] + 20)))
            btn = wx.BitmapButton(self, -1, image, pos = pos, style = wx.FRAME_SHAPED)
            btn.Enable(False)
            self.list_btnSelection.append(btn)
            index = index + 1
    '''
    function _005 : create man image of current selection
    '''
    def _005_createPeopleImg(self):
        image = PublicMethod.ACT001_getBmpImage(LIST_PEOPLE_IMG[0] + "/" + LIST_PEOPLE_IMG[1])
        posBtn = (self.list_btnSelection[0].GetPosition()[0] - 50, self.list_btnSelection[0].GetPosition()[1])
        self.imgMan = wx.BitmapButton(self, -1, image, pos = posBtn, style = wx.FRAME_SHAPED)
        self.imgMan.Enable(False)
    '''
    function _006 : create image of title
    '''
    def _006_createTitleImg(self):
        interval = 20
        #load image
        image_diamond = PublicMethod.ACT001_getBmpImage(FILE_NAME_DIAMOND)
        image_title = PublicMethod.ACT001_getBmpImage(FILE_NAME_TITLE)
        #create button of diamond
        pos_diamond = ((self.GetSize()[0] - image_diamond.GetSize()[0] - interval - image_title.GetSize()[0]) / 2, 30)
        self.imgDiamond = wx.BitmapButton(self, -1, image_diamond, pos = pos_diamond, style = wx.FRAME_SHAPED)
        self.imgDiamond.Enable(False)
        #create button of title
        pos_title = (self.imgDiamond.GetPosition()[0] + self.imgDiamond.GetSize()[0] + interval, 50)
        self.imgTitle = wx.BitmapButton(self, -1, image_title, pos = pos_title, style = wx.FRAME_SHAPED)
        self.imgTitle.Enable(False)
    '''
    function _007 : do according to current selection
    '''
    def _007_OnClickFunction(self):
        if self.curChooseSel == SEL_NEW_GAME :  #select selection - new game
            self._GameFunc001_NewGame()
        elif self.curChooseSel == SEL_EDIT_MAP :  #select selection - edit map
            self._GameFunc002_EditMap()
        elif self.curChooseSel == SEL_OPTION :  #select selection - option
            self._GameFunc003_Option()
        elif self.curChooseSel == SEL_HELP :  #select selection - help
            self._GameFunc004_Help()
        elif self.curChooseSel == SEL_QUIT :  #select selection - quit
            self._GameFunc005_Leave()
    '''
    function _008 : destroy all object of this class
    '''
    def _008_Destroy(self):
        #clear timer
        self.timer_manAnimation.Stop()
        self.timer_manAnimation.Destroy()
        #clear image object
        self.imgBackground.Destroy()
        self.imgDiamond.Destroy()
        self.imgMan.Destroy()
        self.imgTitle.Destroy()
        #clear selection
        index = 0
        while index < self.list_btnSelection.__len__() :
            self.list_btnSelection[index].Destroy()
            index = index + 1
        self.Destroy()
    '''
    function GameFunc001 : start a new game
    '''
    def _GameFunc001_NewGame(self):
        main = self.main
        from Frame_NewGame import Frame_NewGame
        self._008_Destroy()
        Frame_NewGame(main)
    '''
    function GameFunc002 : edit map
    '''
    def _GameFunc002_EditMap(self):
        main = self.main
        from Frame_EditMap import Frame_EditMap
        self._008_Destroy()
        Frame_EditMap(main)
    '''
    function GameFunc003 : open frame of option
    '''
    def _GameFunc003_Option(self):
        main = self.main
        from Frame_Option import Frame_Option
        self._008_Destroy()
        Frame_Option(main)
    '''
    function GameFunc004 : open frame of help
    '''
    def _GameFunc004_Help(self):
        main = self.main
        from Frame_Help import Frame_Help
        self._008_Destroy()
        Frame_Help(main)
    '''
    function GameFunc005 : quit game
    '''
    def _GameFunc005_Leave(self):
        self.main.Close()
    '''
    function SLOT001 : slot of clicking on the keyboard
    '''
    def SLOT001_OnKeyClick(self, event):
        index_preBtn = -1
        index_curBtn = -1
        key = event.GetKeyCode()
        if key == wx.WXK_UP :  #press key - Up
            index_preBtn = self.curChooseSel
            self.curChooseSel = self.curChooseSel - 1
            if self.curChooseSel < 0 :
                self.curChooseSel = LIST_SELECTION_IMG.__len__() - 1
            index_curBtn = self.curChooseSel
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == wx.WXK_DOWN :  #press key - Down
            index_preBtn = self.curChooseSel
            self.curChooseSel = (self.curChooseSel + 1) % LIST_SELECTION_IMG.__len__()
            index_curBtn = self.curChooseSel
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == 13 :  #press key - Enter
            self.main.ACT001_playSound(SOUND_ENTER)  #play sound
            self._007_OnClickFunction()
        if index_curBtn != -1 :
            #turn original selection to be unselected
            self.list_btnSelection[index_preBtn].Show(False)
            imageName = DIR_IMG_MAIN_FRAME + "/" + LIST_SELECTION_IMG[index_preBtn][0]
            image = PublicMethod.ACT001_getBmpImage(imageName)
            self.list_btnSelection[index_preBtn].SetBitmapLabel(image)
            self.list_btnSelection[index_preBtn].Show(True)
            #turn current selection to be selected
            self.list_btnSelection[index_curBtn].Show(False)
            imageName = DIR_IMG_MAIN_FRAME + "/" + LIST_SELECTION_IMG[index_curBtn][1]
            image = PublicMethod.ACT001_getBmpImage(imageName)
            self.list_btnSelection[index_curBtn].SetBitmapLabel(image)
            self.list_btnSelection[index_curBtn].Show(True)
            #move the people image to left of current selection
            self.imgMan.Show(False)
            self.imgMan.Move((self.list_btnSelection[index_curBtn].GetPosition()[0] - 50, 
                              self.list_btnSelection[index_curBtn].GetPosition()[1]))
            self.imgMan.Show(True)
    '''
    function SLOT002 : slot of change image of man
    '''
    def SLOT002_animationMan(self, event):
        self.imgMan.Show(False)
        self.curManImgIndex = (self.curManImgIndex + 1) % (LIST_PEOPLE_IMG.__len__() - 1)
        image = PublicMethod.ACT001_getBmpImage(LIST_PEOPLE_IMG[0] + "/" + LIST_PEOPLE_IMG[self.curManImgIndex + 1])
        self.imgMan.SetBitmapLabel(image)
        self.imgMan.Show(True)